﻿using System;
using System.Runtime.InteropServices.Automation;

namespace ComToolkit.Data
{
    //MSDN: http://msdn.microsoft.com/en-us/library/ms675083%28VS.85%29.aspx
    public class AdoConnection : IDisposable
    {

        internal dynamic adoConnection;
        
        public AdoConnection(string connectionString = "")
        {
            //TODO: Check if windows.
            adoConnection = AutomationFactory.CreateObject("ADODB.Connection");
            ConnectionString = connectionString;
        }

        public string ConnectionString
        {
            get { return adoConnection.ConnectionString; }
            set { adoConnection.ConnectionString = value; }
        }

        public AdoState State 
        {
            get { return (AdoState)adoConnection.State; } 
        }

        public void Open()
        {
            adoConnection.Open();
        }

        public void Close()
        {
            if (this.State != AdoState.Closed)
                adoConnection.Close();
        }

        public AdoCommand CreateCommand()
        {
            return new AdoCommand(this);
        }

        public void Dispose()
        {
            Close();
            adoConnection = null;
        }
    }
}
